package com.mo.ld.activity.interactive

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.mo.ld.R
import com.mo.ld.databinding.ActCamraBinding
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.showToast
import com.mo.lib.utils.handlerImageChooseResult


/**
 * @ author：mo
 * @ data：2021/12/20:17:27
 * @ 功能：
 */
class CamraActivity : BaseVbActivity<ActCamraBinding>() {
    var adapter: BaseQuickAdapter<String,BaseViewHolder>? = null

    override fun onClickView(view: View) {
        super.onClickView(view)
        when (view.id) {
            R.id.tv_camra      -> {
                val intent = Intent()
                intent.type = "image/*"
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
                intent.action = Intent.ACTION_GET_CONTENT
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1024)
            }
            R.id.tv_camra_one  -> {
                showToast("点击了22222")
            }
            R.id.tv_camra_much -> {
                showToast("点击了22222")
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)//        viewBinding.tvCamra.setClick {
        //            val intent = Intent()
        //            intent.type = "image/*"
        //            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        //            intent.action = Intent.ACTION_GET_CONTENT
        //            startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1024)
        //        }
        viewBinding.krvCamra.adapter = object : BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_img) {
            override fun convert(holder: BaseViewHolder, item: String) {
                val view = holder.getView<ImageView>(R.id.iv_item_img)
                Glide.with(this@CamraActivity).load(item).into(view)
            }
        }.also { adapter = it }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val fileList: MutableList<String> = ArrayList()
        if (data != null) {
            val files = data.clipData
            if (files != null) {
                for (i in 0 until files.itemCount) {
                    val fileUri: Uri = files.getItemAt(i).uri //                    fileList.add(fileUri.toString())
                    //                    PathUtils.getFilePathByUri(fileUri)
                    //                    fileList.add(PathUtils.getFilePathByUri(this,fileUri))
                    handlerImageChooseResult(data)?.let { fileList.add(it) }
                }
            } else {
                fileList.add(data.data.toString())
            }
        }
        viewBinding.tvCamra.text = fileList.toString()
        adapter?.setNewInstance(fileList)
    }


    override fun initData() {
    }
}